<template>
  <div class="header_btn">
    <el-button plain @click="onAuth">小程序授权</el-button>
    <el-button plain @click="onBatchDelete" :disabled="!checksData.length">批量删除</el-button>
    <el-button type="primary" @click="onCreateLink">创建短链</el-button>
  </div>
  <div class="table_box">
    <!-- <el-table :data="tableData" stripe style="width: 100%" class="zw-table" @selection-change="handleSelectionChange" :header-cell-style="{ backgroundColor: '#f8f8f8', color: '#333333' }"> -->
    <el-table v-if="tableData.length" :data="tableData" stripe class="zw-table" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="title" label="链接标题" />
      <el-table-column prop="created_name" label="创建人" />
      <el-table-column prop="created_at" label="创建时间" />
      <el-table-column prop="address" label="操作" width="140" fixed="right">
        <template #default="scope">
          <el-button size="small" @click="onCopy(scope.row)">复制</el-button>
          <el-button size="small" @click="onDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-empty v-else description="暂无数据" />
  </div>
  <div class="dialog_box">
    <el-dialog v-model="dialogFormVisible" width="30%" title="小程序授权">
      <el-form label-width="auto">
        <el-form-item>
          <template #label>
            <span>
              小程序appid
              <el-popover placement="top" :width="200" trigger="hover" content="小程序唯一凭证，可在 【微信公众号平台-设置-开发设置】 页面获取">
                <template #reference>
                  <img src="@/assets/imgs/common/question_icon.png" alt="" class="question_icon" />
                </template>
              </el-popover>
            </span>
          </template>
          <el-input v-model="formData.applet_app_id" placeholder="请输入小程序appid" />
        </el-form-item>
        <el-form-item>
          <template #label>
            <span>
              小程序AppScret
              <el-popover placement="top" :width="200" trigger="hover" content="小程序唯一凭证，可在 【微信公众号平台-设置-开发设置】 页面获取">
                <template #reference>
                  <img src="@/assets/imgs/common/question_icon.png" alt="" class="question_icon" />
                </template>
              </el-popover>
            </span>
          </template>
          <el-input v-model="formData.applet_app_secret" placeholder="请输入小程序AppScret" />
        </el-form-item>
      </el-form>
      <div class="wechat_box">
        <div class="wechat_tit">使用引流连接功能前，请联系我们！</div>
        <img class="wechat_img" :src="wechat" alt="">
        <div class="wechat_desc">扫码添加客服</div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel">取消</el-button>
          <el-button type="primary" @click="onSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import useClipboard from 'vue-clipboard3'
  import { getLinkList, getApplet, modifyApplet } from '@/ajax/api/messages/marketing/drainageLink'
  import wechat from '@/assets/imgs/wechat.png'
  interface Props {
    tableData?: any
    checksData?: any
    isAuthApplet?: number
  }
  const props = withDefaults(defineProps<Props>(), {
    tableData: [],
    checksData: [],
    isAuthApplet: 0
  })
  const emit = defineEmits(['submit'])
  const dialogFormVisible = ref(false)
  const formData: any = ref({
    applet_app_id: '',
    applet_app_secret: ''
  })

  const handleSelectionChange = (v: any[]) => {
    emit('submit', 5, v)
  }
  const onBatchDelete = () => {
    ElMessageBox.confirm('确定删除所选内容吗？', '删除提醒', {
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    }).then(() => {
      emit('submit', 2)
    })
  }
  const onCreateLink = () => {
    if (props.isAuthApplet == 1) {
      emit('submit', 3)
    } else {
      ElMessage.warning('请先授权小程序')
    }
  }
  const onDelete = (row: object) => {
    ElMessageBox.confirm('确定删除所选内容吗？', '删除提醒', {
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    }).then(() => {
      emit('submit', 4, row)
    })
  }
  // 复制
  const { toClipboard } = useClipboard()
  const onCopy = async (v: any) => {
    try {
      await toClipboard(v.attract_url)
      ElMessage.success('复制成功')
    } catch (e) {
      ElMessage.error('复制失败')
    }
  }
  const onAuth = async () => {
    const res: any = await getApplet()
    console.log(res, '2323123131231')
    dialogFormVisible.value = true
    formData.value = {
      applet_app_id: res.applet_app_id,
      applet_app_secret: res.applet_app_secret
    }
  }
  const onSubmit = async () => {
    await modifyApplet(formData.value)
    ElMessage.success('修改成功')
    dialogFormVisible.value = false
  }
  const onCancel = () => {
    dialogFormVisible.value = false
    formData.value = {
      applet_app_id: '',
      formData: ''
    }
  }
</script>

<style lang="scss" scoped>
  .el-popper {
    :deep(.is-light) {
      background-color: red;
    }
  }
  .header_btn {
    margin-top: 10px;
    padding-right: 16px;
    text-align: right;
  }
  .table_box {
    margin: 26px 0 16px;
  }
  .dialog_box {
    :deep(.el-dialog__header) {
      border-bottom: 1px solid #eaeaea;
    }
    :deep(.el-dialog__body) {
      padding-right: 55px;
      border-bottom: 1px solid #eaeaea;
    }
    .wechat_box{
      margin-top: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      .wechat_img{
        margin:15px;
        width:200px;
      }
    }
  }
  //
  .question_icon {
    width: 16px;
  }
</style>
